<?php

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

/**
 * 访问控制器方法
 */
class Action extends Command
{

    //命令行访问控制器，可跳过路由
    //实例 php think action admin/test/index --option name=jack,age=10

    /**
     * 指令配置
     * @return void
     */
    protected function configure()
    {
        $this->setName("action")
            ->addArgument("route", Argument::OPTIONAL, "your run route path!") //路由地址必输
            ->addOption('option', 'o', Option::VALUE_REQUIRED, 'set Controller Argument')
            ->setDescription("Command run Controller Action !");
    }

    protected function execute(Input $input, Output $output)
    {
        $Argument = $input->getArguments();
        if ($Argument['command'] == 'action') {
            if ($input->hasOption('option')) {
                $params    = $this->option($input->getOption('option'));
                $class_fun = $this->route($Argument['route']);
                $fun       = $class_fun['fun'];
                $result    = app("{$class_fun['app']}\\{$class_fun['module']}\\{$class_fun['controller']}\\{$class_fun['class']}")->$fun($params);
                $output->writeln(is_string($result)?$result:json_encode($result, true));
            } else {
                $class_fun = $this->route($Argument['route']);
                $fun    = $class_fun['fun'];
                $result = app("{$class_fun['app']}\\{$class_fun['module']}\\{$class_fun['controller']}\\{$class_fun['class']}")->$fun();
                $output->writeln(is_string($result)?$result:json_encode($result, true));
            }
        }

    }

    private function route($route = '')
    {
        $class_fun['class'] = "app\\index\\controller\\index";
        $class_fun['fun']   = "index";

        if ($route) {
            $app        = "app";
            $controller = "controller";
            $route      = explode("/", $route);
            $module     = $route[0]??'index'; //模块
            $class      = $route[1]??'index'; //类 兼容文件夹
            $fun        = $route[2]??'index'; //方法

            if(strpos($class, ".") === false) { //兼容文件夹间隔
                $class_fun['class'] = ucfirst($class);
            } else {
                $arr = explode(".", $class);
                $arr[1] = ucfirst($arr[1]);
                $class_fun['class'] = implode("\\", $arr);
            }

            $class_fun['fun']        = $fun;
            $class_fun['module']     = $module;
            $class_fun['app']        = $app;
            $class_fun['controller'] = $controller;
        }
        return $class_fun;
    }

    private function option($option)
    {
        $params     = array();
        $option_arr = explode(",", $option);
        foreach ($option_arr as $key => $val) {
            $temp_params             = explode("=", $val);
            $params[$temp_params[0]] = $temp_params[1];
        }
        return $params;
    }
}